package cn.backpackerxl.servlet;

import cn.backpackerxl.entity.Student;
import cn.backpackerxl.pojo.PageStudent;
import cn.backpackerxl.service.StudentService;
import cn.backpackerxl.service.impl.StudentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

import static cn.backpackerxl.util.CodeToLong.getCodeToLong;

@WebServlet(name = "StudentServlet", value = "/student")
public class StudentServlet extends HttpServlet {
    private final StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        //获取不同的响应操作
        String action = request.getParameter("action");

        //使用反射机制执行不同的响应事件
        try {
            Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理搜索请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String key = request.getParameter("key");
        PageStudent pageStudents = studentService.findByName(key, 1, 8);
        request.setAttribute("pageStudent", pageStudents);
        request.setAttribute("key", key);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    /**
     * 处理删除请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String current = request.getParameter("current");
        int i = studentService.deleteStudent(Integer.parseInt(id));
        if (i == 0) {
            response.sendRedirect(request.getContextPath() + "/page?action=pageOption&currentPage=" + current.trim() + "&info=error");
        } else {
            response.sendRedirect(request.getContextPath() + "/page?action=pageOption&currentPage=" + current.trim() + "&info=success");
        }
    }

    /**
     * 处理修改的请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String stuId = request.getParameter("stu_id");
        String age = request.getParameter("age");
        String height = request.getParameter("height");
        String weight = request.getParameter("weight");
        String hobby = request.getParameter("hobby");
        String current = request.getParameter("current");
        String iSex = sex.trim().equals("男") ? "0" : "1";
        Student student = new Student(Integer.parseInt(id), stuId, name.trim(), iSex, Integer.parseInt(age.trim()), Integer.parseInt(height.trim()), Integer.parseInt(weight.trim()), hobby.trim());
        Student copyStudent = studentService.findById(student.getId());
        copyStudent.setSex(Objects.equals(copyStudent.getSex(), "男") ? "0" : "1");
        if (student.equals(copyStudent)) {
            response.sendRedirect(request.getContextPath() + "/page?action=pageOption&currentPage=" + current.trim() + "&info=warn");
        } else {
            int i = studentService.updateStudent(student);
            if (i == 0) {
                response.sendRedirect(request.getContextPath() + "/page?action=pageOption&currentPage=" + current.trim() + "&info=error");
            } else {
                response.sendRedirect(request.getContextPath() + "/page?action=pageOption&currentPage=" + current.trim() + "&info=success");
            }
        }
    }

    /**
     * 处理添加请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String height = request.getParameter("height");
        String weight = request.getParameter("weight");
        String hobby = request.getParameter("hobby");
        Student student = new Student();
        student.setName(name);
        student.setSex(sex);
        student.setStuId(String.valueOf(getCodeToLong(9)));
        student.setAge(Integer.parseInt(age));
        student.setHeight(Integer.parseInt(height));
        student.setWeight(Integer.parseInt(weight));
        student.setHobby(hobby);
        int i = studentService.addStudent(student);
        if (i == 0) {
            response.sendRedirect(request.getContextPath() + "/page?action=pageAll&currentPage=1&info=error");
        } else {
            response.sendRedirect(request.getContextPath() + "/page?action=pageAll&currentPage=1&info=success");
        }
    }
}
